﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace IFramework
{
    public static partial class StringExtension
    {
        public static string ToSha1Base64(this string value)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(value));
            return Convert.ToBase64String(bytes);
        }

        public static string ToMd5(this string value)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
            return Convert.ToBase64String(bytes);
        }
        public static int ToInt(this string self, int defaulValue = 0)
        {
            var retValue = defaulValue;
            return int.TryParse(self, out retValue) ? retValue : defaulValue;
        }
        public static DateTime ToDateTime(this string self, DateTime defaultValue = default(DateTime))
        {
            var retValue = defaultValue;
            return DateTime.TryParse(self, out retValue) ? retValue : defaultValue;
        }
        public static float ToFloat(this string self, float defaulValue = 0)
        {
            var retValue = defaulValue;
            return float.TryParse(self, out retValue) ? retValue : defaulValue;
        }    
    }
}
